﻿//*************************************************************************************************
// A validation rule used to that two context parameters have the same value.
//
// Copyright(c) Microsoft Corporation, 2008
//*************************************************************************************************

using System;
using System.Globalization;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace WebAndLoadTestPluginsAndRules
{
    [System.ComponentModel.DisplayName("CompareContextParams")]
    [System.ComponentModel.Description("Validation rule to simply validate that two context params are set to the same value")]
    public class CompareContextParams : ValidationRule
    {

        /// <summary>
        /// standard constructor
        /// </summary>
        public CompareContextParams() { }

        private string m_ContextParam1;
        private string m_ContextParam2;

        public string ContextParam1
        {
            get { return m_ContextParam1; }
            set { m_ContextParam1 = value; }
        }
        public string ContextParam2
        {
            get { return m_ContextParam2; }
            set { m_ContextParam2 = value; }
        }

        internal bool ValidateContextParam(WebTestContext context, string contextParamName, out string value, out string message)
        {
            if (!context.Keys.Contains(contextParamName) || !(context[contextParamName].GetType() == typeof(System.String)))
            {
                message = String.Format(CultureInfo.CurrentCulture, "ContextParameter '{0}' not set", contextParamName);
                value = "";
                return false;
            }
            message = "";
            value = (string)context[contextParamName];
            return true;
        }

        internal bool Validate(WebTestContext context, out string message)
        {

            bool isValid;
            try
            {
                string string1;
                isValid = ValidateContextParam(context, ContextParam1, out string1, out message);
                if (!isValid)
                    return isValid;

                string string2;
                isValid = ValidateContextParam(context, ContextParam2, out string2, out message);
                if (!isValid)
                {
                    return isValid;
                }

                isValid = string.Compare(string1, string2) == 0;
                if (!isValid)
                    message = String.Format(CultureInfo.CurrentCulture, "{0} != {1}", string1, string2);
                return isValid;
            }
            catch (Exception ex)
            {
                message = String.Format(CultureInfo.CurrentCulture, "{CompareContextParams} Exception: {0}", ex.Message.ToString());
                return false;
            }
        }

        public override void Validate(object sender, ValidationEventArgs e)
        {
            string message;
            e.IsValid = Validate(e.WebTest.Context, out message);
            e.Message = message;
        }
    }
}
